package com.sc.web.gwt.client.view.uicomponent;

/**
 * User: KienCT
 * Date: 22/07/2011
 * Time: 09:56
 */

import com.google.gwt.dom.client.Style;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.TextBox;

/**
 * a utility class to display a small image tag next to a field to determine if that field is valid or not
 */
public class TextBoxValidationHelper {

    private Image image = new Image();
    private TextBox textBox;
    private boolean isErrorShown = false;
    private InlineLabel label = new InlineLabel();

    public TextBoxValidationHelper(TextBox textBox) {
        this.textBox = textBox;
    }

    public void showErrorOnTextBox(final String message) {
        clearStatusOnTextBox();
        label = new InlineLabel(message);
        label.getElement().getStyle().setDisplay(Style.Display.BLOCK);
        label.getElement().getStyle().setMarginTop(2, Style.Unit.PX);
        label.getElement().getStyle().setColor("brown");
        label.getElement().getStyle().setFontWeight(Style.FontWeight.BOLD);
        this.textBox.getElement().getParentElement().insertAfter(label.getElement(), this.textBox.getElement());
        isErrorShown = true;
    }

    public void showOkOnTextBox() { }

    public void clearStatusOnTextBox() {
        if(isErrorShown){
            label.getElement().removeFromParent();
            isErrorShown = false;
        }
    }
}
